package JPetStore.web.servlet;

import JPetStore.domain.Account;
import JPetStore.service.AccountService;
import JPetStore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ModifyInformationServlet extends HttpServlet {
    private static final String INFORMATION = "/WEB-INF/jsp/account/AccountInformation.jsp";
    private Account account;

    public ModifyInformationServlet() {
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        cloneAccount((Account)session.getAttribute("loginAccount"));
//        this.account.setUsername((String)session.getAttribute("username"));
        this.account.setPhone(req.getParameter("phone"));
        this.account.setZip(req.getParameter("zip"));
        this.account.setAddress1(req.getParameter("address1"));
        this.account.setAddress2(req.getParameter("address2"));
        this.account.setState(req.getParameter("state"));
        this.account.setStatus(req.getParameter("status"));
        this.account.setFirstName(req.getParameter("firstname"));
        this.account.setLastName(req.getParameter("lastname"));
        this.account.setCountry(req.getParameter("country"));
        this.account.setCity(req.getParameter("city"));
        this.account.setEmail(req.getParameter("email"));
//        System.out.println(this.account.getUsername());
        AccountService service = new AccountService();
        service.changeInformation(this.account);
        this.account.setPassword((String)session.getAttribute(this.account.getPassword()));
        session.setAttribute("loginAccount", this.account);
        Account loginAccount = (Account)session.getAttribute("loginAccount");
        if (loginAccount != null) {
            String username = loginAccount.getUsername();
            String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + req.getContextPath() + req.getServletPath() + "?" + (req.getQueryString());
            LogService logService = new LogService();
            String logInfo = logService.logInfo(new Object[]{" "}) + strBackUrl + "修改了个人信息";
            logService.insertLogInfo(username, logInfo);
        }
        req.getRequestDispatcher(INFORMATION).forward(req, resp);
    }
    private void cloneAccount(Account a)
    {
        this.account = new Account();
        this.account.setUsername(a.getUsername());
        this.account.setPhone(a.getPhone());
        this.account.setZip(a.getZip());
        this.account.setAddress1(a.getAddress1());
        this.account.setAddress2(a.getAddress2());
        this.account.setState(a.getState());
        this.account.setStatus(a.getStatus());
        this.account.setFirstName(a.getFirstName());
        this.account.setLastName(a.getLastName());
        this.account.setCountry(a.getCountry());
        this.account.setCity(a.getCity());
        this.account.setEmail(a.getEmail());
    }
}
